﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;
using WindowsForms.service;
using WindowsForms.Class;

namespace WindowsForms
{
    public partial class StaffManagement : DevExpress.XtraEditors.XtraForm
    {
        Service1 iQuery;
        private List<Staffs> lstStaffSource = null;

        public StaffManagement()
        {
            iQuery = new Service1();
            InitializeComponent();
            GetAllStaff();
        }

        private void GetAllStaff()
        {
            ConvertToList convert = new ConvertToList();
            
            lstStaffSource = convert.ConvertTolistStaffs(iQuery.SelectStaffsAll().Tables[0]);
            gridControl1.DataSource = lstStaffSource;
        }

        private void btnAddItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (StaffActionAEProcess frm = new StaffActionAEProcess())
            {
                if (frm.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    Staffs stEntity = new Staffs()
                    {
                        UserName = frm.UserName,
                        Password = frm.Password,
                        StaffName = frm.StaffName,
                        Address = frm.Address,
                        BirthDate = frm.BirthDate,
                        Gender = frm.Gender,
                        NoNumber = frm.NoNumber,
                        PhoneNumber = frm.PhoneNumber,
                        StartWorkDate = frm.StartWorkDate,
                        EndWorkDate = frm.EndWorkDate,
                        CreateDate = DateTime.Now,
                        Enable = frm.Enable,
                        RoleID = frm.RoleId,
                        IMEI = frm.IMEI
                    };
                    iQuery.InsertStaff(stEntity.RoleID, stEntity.UserName, stEntity.Address, stEntity.BirthDate, stEntity.Gender, stEntity.NoNumber,
                        stEntity.PhoneNumber, stEntity.StartWorkDate, stEntity.EndWorkDate, stEntity.CreateDate, stEntity.Enable, stEntity.Password, stEntity.StaffName, stEntity.IMEI);

                    gvStaff.RefreshData();
                }
            }
        }

        private void btnEditItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int[] rows = gvStaff.GetSelectedRows();
            Staffs staffObj = gvStaff.GetRow(rows[rows.Length - 1]) as Staffs;
            using (StaffActionAEProcess frm = new StaffActionAEProcess(true))
            {
                frm.UserName = staffObj.UserName;                
                frm.StaffName = staffObj.StaffName;
                frm.Address = staffObj.Address;
                frm.BirthDate = staffObj.BirthDate;
                frm.Gender = staffObj.Gender;
                frm.NoNumber = staffObj.NoNumber;
                frm.PhoneNumber = staffObj.PhoneNumber;
                frm.StartWorkDate = staffObj.StartWorkDate;
                frm.EndWorkDate = staffObj.EndWorkDate;
                frm.Enable = staffObj.Enable;
                frm.RoleId = staffObj.RoleID;
                frm.IMEI = staffObj.IMEI;

                if (frm.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    staffObj.UserName = frm.UserName;
                    if (!String.IsNullOrEmpty(frm.Password))
                    {
                        staffObj.Password = frm.Password;
                    }

                    staffObj.StaffName = frm.StaffName;
                    staffObj.Address = frm.Address;
                    staffObj.BirthDate = frm.BirthDate;
                    staffObj.Gender = frm.Gender;
                    staffObj.NoNumber = frm.NoNumber;
                    staffObj.PhoneNumber = frm.PhoneNumber;
                    staffObj.StartWorkDate = frm.StartWorkDate;
                    staffObj.EndWorkDate = frm.EndWorkDate;
                    staffObj.Enable = frm.Enable;
                    staffObj.RoleID = frm.RoleId;
                    staffObj.IMEI = frm.IMEI;

                    iQuery.UpdateStaff(staffObj.StaffID, staffObj.RoleID, staffObj.UserName, staffObj.Address, staffObj.BirthDate, staffObj.Gender, staffObj.NoNumber, staffObj.PhoneNumber
                        , staffObj.StartWorkDate, staffObj.EndWorkDate, staffObj.CreateDate, staffObj.Enable, staffObj.Password, staffObj.StaffName, staffObj.IMEI);
                    gvStaff.RefreshData();
                }
            }
        }

        private void btnDelItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int[] rows = gvStaff.GetSelectedRows();
            Staffs staffObj = gvStaff.GetRow(rows[rows.Length - 1]) as Staffs;
            if (XtraMessageBox.Show(string.Format(@"Có muốn xóa ""{0}"" không?", staffObj.StaffName),
                    "Confirmation", MessageBoxButtons.YesNo) != DialogResult.No)
            {
                iQuery.DeleteStaff(staffObj.StaffID);
                lstStaffSource.Remove(staffObj);
                gvStaff.RefreshData();
            }
        }

        private void gvStaff_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            btnEditItem.Enabled = gvStaff.SelectedRowsCount > 0;
            btnDelItem.Enabled = gvStaff.SelectedRowsCount > 0;
        }

        private void StaffManagement_Load(object sender, EventArgs e)
        {

        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            GetAllStaff();
        }
    }
}